package com.firmboy.log.base.user.service;

import com.firmboy.log.base.bean.BaseMsg;
import com.firmboy.log.base.bean.PageParam;
import com.firmboy.log.base.user.dao.UserDao;
import com.firmboy.log.base.user.model.User;
import com.firmboy.log.util.UUIDutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * user的业务层
 *
 * @author firmboy
 * @create 2018-03-17 下午2:51
 **/
@Service
@Transactional
public class UserService {

    @Autowired
    private UserDao userDao;

    public BaseMsg getUsers(){
        List<User> all = userDao.all();
        BaseMsg msg = BaseMsg.successMsg();
        msg.setData(all);
        return msg;
    }

    public BaseMsg getUserById(String id){
        User user = userDao.single(id);
        BaseMsg msg = BaseMsg.successMsg();
        msg.setData(user);
        return msg;
    }

    public BaseMsg deleteById(String id){
        int i = userDao.deleteById(id);
        BaseMsg msg = BaseMsg.successMsg();
        msg.setData(i);
        return msg;
    }

    public BaseMsg insertOrUpdateUser(User user){
        if (user.getId()==null){
            user.setId(UUIDutil.getIdByUUID());
            userDao.insert(user);
        }else{
            userDao.updateTemplateById(user);
        }
        BaseMsg msg = BaseMsg.successMsg();
        return msg;
    }

    public BaseMsg page(PageParam<User>  param){
        List<User> users = userDao.getPageByParam(param);
        BaseMsg msg = BaseMsg.successMsg(users);
        return msg;
    }

}
